/*
   Copyright 2020 XiaochengTech

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package wxofficial

import (
	"encoding/json"
	"fmt"

	"gitee.com/xiaochengtech/wechat/constant"
	"gitee.com/xiaochengtech/wechat/util"
)

// 创建自定义菜单接口
// https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Creating_Custom-Defined_Menu.html
func CustomMenuCreate(
	accessToken string,
	req CustomMenuCreateRequest,
) (err error) {
	// 构造请求
	url := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s", accessToken)
	// 发送数据
	rspData, err := util.HttpPostJson(url, req)
	if err != nil {
		return
	}
	var rsp constant.BaseResponse
	if err = json.Unmarshal(rspData, &rsp); err != nil {
		return
	}
	if rsp.ErrCode != constant.ErrCodeSuccess {
		err = fmt.Errorf("%d:%s", rsp.ErrCode, rsp.ErrMsg)
		return
	}
	return
}

type CustomMenuCreateRequest struct {
	Button []CustomMenuItem `json:"button"` // 一级菜单
}
